<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_shipping.php 473 2014-01-30 16:00:59Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 473 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:00:59 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_INC.'inc.parse_shipping.php');

class ot_shipping {
	var $title, $output;
	
	function __construct() {
		global $price, $db;
		$this->code = 'ot_shipping';
		$this->title = MODULE_ORDER_TOTAL_SHIPPING_TITLE;
		$this->description = MODULE_ORDER_TOTAL_SHIPPING_DESCRIPTION;
		$this->enabled = MODULE_ORDER_TOTAL_SHIPPING_STATUS == 'true' ? true : false;
		$this->sort_order = MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER;
		$this->output = array();
		$this->db = $db;
	}
	
  function process($values_from_cart = false, &$order_total_array) {
		global $order, $price;
    $order_total_base = $order_total_array['all'];

    $order_total_base['code'] = $this->code;
    $order_total_base['title'] = $this->title;
    $order_total_base['sort_order'] = $this->sort_order;

		if(defined('MODULE_SHIPPING_FREEAMOUNT_STATUS') && MODULE_SHIPPING_FREEAMOUNT_STATUS == 'True') {
			if($order_total_array['netto'] >= MODULE_SHIPPING_FREEAMOUNT_AMOUNT) {
				$order->info['shipping_method'] = $this->title;
				$order->info['total'] -= $order->info['shipping_cost'];
				$order->info['shipping_cost'] = 0;
				$order_total_base['netto'] = $order_total_base['brutto'] = $order_total_base['value'] = 0;
				$order_total_base['text'] = '-';
				$order_total_array[$this->code] = $order_total_base;
				return;
			}
		}

		list ($module, $method) = parse_shipping($_SESSION['shipping']['id']);

		if(!isset($GLOBALS[$module]) && file_exists(DIR_FS_CATALOG.'includes/modules/shipping/'.basename($module).'.php')) {
			include_once(DIR_FS_CATALOG.'includes/modules/shipping/'.basename($module).'.php');
			$GLOBALS[$module] = new $module;
		}
	
		if (not_null($order->info['shipping_method'])) {
			$order_total_base['title'] = $order->info['shipping_method'];
			$order_total_base['netto'] = $order->info['shipping_cost'];
			if(empty($GLOBALS[$module]->tax_class)) {
				$tax_class = array_keys($order_total_base['tax']);
				$tax_class = $tax_class[0];
				$order_total_base['tax'][$tax_class]['value'] = 0.0;
				$order_total_base['brutto'] = $order_total_base['netto'];
			}		
			else {
				$order_total_base['tax'] = array(
					$GLOBALS[$module]->tax_class => array(
						'id' 		=> $GLOBALS[$module]->tax_class,
						'rate'	=> get_tax_rate($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id'])
					)
				);
				$order_total_base['tax'][$GLOBALS[$module]->tax_class]['value'] 
					= $order_total_base['tax'][$GLOBALS[$module]->tax_class]['rate']/100.0 * $order->info['shipping_cost'];
				$order_total_base['brutto'] = add_tax($order_total_base['netto'], $order_total_base['tax'][$GLOBALS[$module]->tax_class]['rate']);
			}
			
			if ($_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
				$shipping_tax = get_tax_rate($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
				$shipping_tax_description = get_tax_description($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
				$tax = ($shipping_tax/100.0) * $order->info['shipping_cost'];
				$order->info['shipping_cost'] = add_tax($order->info['shipping_cost'], $shipping_tax);
				
				$order->info['tax'] += $tax;
				$order->info['tax_groups'][TAX_ADD_TAX."$shipping_tax_description"] += $tax;
				$tax = $price->format($tax,false,0,true);
				$order->info['total'] += $tax;

				$order_total_base['value'] = $order_total_base['brutto'];
			}
			else
				$order_total_base['value'] = $order_total_base['netto'];
			
			$order_total_base['text'] = $price->format($order_total_base['value'], true, 0, true);
		
			$this->output[] = array(
				'title' => $order->info['shipping_method'].':',
				'text' => $price->format($order->info['shipping_cost'], true, 0, true),
				'value' => $price->format($order->info['shipping_cost'], false, 0, true)
			);

			$order_total_array[$this->code] = $order_total_base;
		}
	}
	
	function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_ORDER_TOTAL_SHIPPING_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}
	
	function keys() {
		return array('MODULE_ORDER_TOTAL_SHIPPING_STATUS', 
					'MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER',
					'MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS');
	}
	
	function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_STATUS', 'true','6', '1','cfg_select_option(array(\'true\', \'false\'), ', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER', '30','6', '2', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS', '0','6', '3', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', NOW())");      
	}
	
	function remove() {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN ('".implode("', '", $this->keys())."')");
	}
}
?>